/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package com.billy.ohos.swipe;

import com.ryan.ohos.extension.event.DispatchHelper;
import com.ryan.ohos.extension.event.impl.ViewGroupHelper;
import com.ryan.ohos.extension.event.interfaces.ViewGroup;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

/**
 * 自定义DependentLayout，处理事件分发
 */
public class CustomDependentLayout extends DependentLayout implements ViewGroup, Component.TouchEventListener {
    private ViewGroupHelper viewGroupHelper;

    public CustomDependentLayout(Context context) {
        this(context, null);
    }

    public CustomDependentLayout(Context context, AttrSet attrSet) {
        this(context, attrSet, "");
    }

    public CustomDependentLayout(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        setTouchEventListener(this);
        viewGroupHelper = new ViewGroupHelper(this);
    }

    @Override
    public void requestDisallowInterceptTouchEvent(boolean b) {
        viewGroupHelper.requestDisallowInterceptTouchEvent(b);
    }

    @Override
    public boolean onInterceptTouchEvent(TouchEvent touchEvent) {
        return false;
    }

    @Override
    public boolean dispatchTouchEvent(TouchEvent touchEvent) {
        return viewGroupHelper.dispatchTouchEvent(touchEvent);
    }

    @Override
    public boolean onTouchEvent(TouchEvent touchEvent) {
        return true;
    }

    @Override
    public boolean isConsumed() {
        return viewGroupHelper.isConsumed();
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        return DispatchHelper.dispatch(component, touchEvent);
    }
}
